class SCALASSIST extends eui.Component implements  eui.UIComponent {
	leftup:eui.Button
	upmid:eui.Button
	rightup:eui.Button
	leftmid:eui.Button
	leftdown:eui.Button
	downmid:eui.Button
	rightdown:eui.Button
	rightmid:eui.Button
	anchor:eui.RadioButton
	fistpos:egret.Point
	mode:string
	ancorX:number
	ancorY:number

	public constructor() {
		super();
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		super.childrenCreated();
		this.fistpos=new egret.Point
		this.leftup.name="leftup"
		this.leftup.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.upmid.name="upmid"
		this.upmid.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.rightup.name="rightup"
		this.rightup.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.leftmid.name="leftmid"
		this.leftmid.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.leftdown.name="leftdown"
		this.leftdown.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.downmid.name="downmid"
		this.downmid.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.rightdown.name="rightdown"
		this.rightdown.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.rightmid.name="rightmid"
		this.rightmid.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		this.anchor.name="anchor"
		this.anchor.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.scalxy,this);
		egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.scalxy,this);

	}
	public scalxy(e:egret.TouchEvent)
	{
	
	  if(e.type==egret.TouchEvent.TOUCH_BEGIN)
	  {
		this.ancorX=0;
		  this.ancorY=0;
        this.fistpos.x=e.stageX
	    this.fistpos.y=e.stageY
		this.mode=e.target.name
		egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.scalxy,this);
		egret.MainContext.instance.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.scalxy,this);
		return;
	  }
      if(e.type==egret.TouchEvent.TOUCH_END)
	  {
		  if(this.mode=="anchor") //锚点改变的节点
		  {
			   CMainEditor.editor.goal.anchorOffsetX=this.ancorX
		       CMainEditor.editor.goal.anchorOffsetY=this.ancorY
			    this.resetpos()
		  }
		  
		  egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.scalxy,this);
		  egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.scalxy,this);
		  return;
	  }
       let nowpos=new egret.Point(e.stageX,e.stageY)
	   let target=CMainEditor.editor.goal
	   if(this.mode=="leftup")
	   {
         let vector=this.fistpos.subtract(nowpos)
		 CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		 CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
		 CMainEditor.editor.goal.x-=vector.x
		 CMainEditor.editor.goal.y-=vector.y
         this.fistpos=nowpos
		this.resetpos()
	   }
	   else if(this.mode=="upmid")
	   {
          let vector=this.fistpos.subtract(nowpos)
		 CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
		  CMainEditor.editor.goal.y-=vector.y
          this.fistpos=nowpos
		this.resetpos()
	   }
	    else if(this.mode=="rightup")
	   {
		 let vector=new egret.Point(nowpos.x-this.fistpos.x,this.fistpos.y-nowpos.y)
	
		 CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		 CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
		 
		 CMainEditor.editor.goal.y-=vector.y
          this.fistpos=nowpos
		this.resetpos()
	   }
	    else if(this.mode=="leftmid")
	   {
		 let vector=this.fistpos.subtract(nowpos)
		 CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		  CMainEditor.editor.goal.x-=vector.x
          this.fistpos=nowpos
         this.resetpos()
		
	   }
	    else if(this.mode=="leftdown")
	   {
		 let vector=new egret.Point(this.fistpos.x-nowpos.x,nowpos.y-this.fistpos.y)
		  CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		  CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
		  CMainEditor.editor.goal.x-=vector.x
          this.fistpos=nowpos
		 this.resetpos()
	   }

	    else if(this.mode=="downmid")
	   {
		 let vector=new egret.Point(this.fistpos.x-nowpos.x,nowpos.y-this.fistpos.y)
		 CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
         this.fistpos=nowpos
		 this.resetpos()
	   }
	    else if(this.mode=="rightdown")
	   {
		 let vector=new egret.Point(nowpos.x-this.fistpos.x,nowpos.y-this.fistpos.y)
		 CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		 CMainEditor.editor.goal.scaleY=(CMainEditor.editor.goal.height*CMainEditor.editor.goal.scaleY+vector.y)/CMainEditor.editor.goal.height;
          this.resetpos()
		 this.fistpos=nowpos
	   }
	    else if(this.mode=="rightmid")
	   {
		let vector=new egret.Point(nowpos.x-this.fistpos.x,nowpos.y-this.fistpos.y)
		 CMainEditor.editor.goal.scaleX=(CMainEditor.editor.goal.width*CMainEditor.editor.goal.scaleX+vector.x)/CMainEditor.editor.goal.width;
		  this.fistpos=nowpos
		 this.resetpos()
		 
	   }
	   	else if(this.mode=="anchor")
	   {
		   let local=CMainEditor.editor.goal.globalToLocal(e.stageX,e.stageY)
		   this.anchor.x=nowpos.x
		   this.anchor.y=nowpos.y
		   this.ancorX=local.x
		   this.ancorY=local.y
	   }
	}
	public resetpos()
	{
		let target=CMainEditor.editor.goal;//得到当前对象
	
		let targetpos=CMainEditor.editor.CurrentGoalstagexy;//得到当前对象的舞台坐标
		this.anchor.x=targetpos.x
		this.anchor.y=targetpos.y
		//去锚点得左上角坐标
		targetpos.x-=target.anchorOffsetX*target.scaleX;
		targetpos.y-=target.anchorOffsetY*target.scaleY;
		//开始设置额8个按钮位置
		this.leftup.x=targetpos.x-this.leftup.width;
		this.leftup.y=targetpos.y-this.leftup.height;

		this.upmid.x=targetpos.x+target.width*target.scaleX/2
		this.upmid.y=this.leftup.y

		this.rightup.x=targetpos.x+target.width*target.scaleX
		this.rightup.y=this.leftup.y

        this.rightmid.x=this.rightup.x
		this.rightmid.y=targetpos.y+target.height*target.scaleY/2

		this.rightdown.x=this.rightup.x
		this.rightdown.y=targetpos.y+target.height*target.scaleY;

		this.downmid.x=this.upmid.x
		this.downmid.y=this.rightdown.y

		this.leftdown.x=this.leftup.x
		this.leftdown.y=this.rightdown.y

		this.leftmid.x=this.leftup.x
		this.leftmid.y=this.rightmid.y
        
		if(target.rotation)  //旋转
		{
		
			let a=target.localToGlobal(target.anchorOffsetX,target.anchorOffsetY)
			let leftuppoint=this.Pointrevolve(this.leftup.x,this.leftup.y,a,target.rotation)
			this.leftup.x=leftuppoint.x
			this.leftup.y=leftuppoint.y
			let upmidpoint=this.Pointrevolve(this.upmid.x,this.upmid.y,a,target.rotation)
			this.upmid.x=upmidpoint.x
			this.upmid.y=upmidpoint.y
			let rightuppoint=this.Pointrevolve(this.rightup.x,this.rightup.y,a,target.rotation)
			this.rightup.x=rightuppoint.x
			this.rightup.y=rightuppoint.y
			let rightmidpoint=this.Pointrevolve(this.rightmid.x,this.rightmid.y,a,target.rotation)
			this.rightmid.x=rightmidpoint.x
			this.rightmid.y=rightmidpoint.y
			let rightdownpoint=this.Pointrevolve(this.rightdown.x,this.rightdown.y,a,target.rotation)
			this.rightdown.x=rightdownpoint.x
			this.rightdown.y=rightdownpoint.y
			let downmidpoint=this.Pointrevolve(this.downmid.x,this.downmid.y,a,target.rotation)
			this.downmid.x=downmidpoint.x
			this.downmid.y=downmidpoint.y
			let leftdownpoint=this.Pointrevolve(this.leftdown.x,this.leftdown.y,a,target.rotation)
			this.leftdown.x=leftdownpoint.x
			this.leftdown.y=leftdownpoint.y
			let leftmidpoint=this.Pointrevolve(this.leftmid.x,this.leftmid.y,a,target.rotation)
			this.leftmid.x=leftmidpoint.x
			this.leftmid.y=leftmidpoint.y
		
		}



	}
	protected Pointrevolve(x:number,y:number,o:egret.Point,angle:number):egret.Point //一点绕任意一点旋转任意度数
	{
		 
		 var point =new egret.Point
		 point.x=(x-o.x)*Math.cos(angle*Math.PI / 180)-(y-o.y)*Math.sin(angle*Math.PI / 180)+o.x
		 point.y=(x-o.x)*Math.sin(angle*Math.PI/180)+(y-o.y)*Math.cos(angle*Math.PI / 180)+o.y
		
		 return point
	}
	
}